package com.fit.WatchShop.pages;

import java.util.Collection;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.beaneditor.Validate;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.WatchShop.beans.BuyBean;
import com.fit.WatchShop.interfaces.Buy;
import com.fit.WatchShop.interfaces.Buyer;
import com.fit.WatchShop.interfaces.User;
import com.fit.WatchShop.interfaces.Watch;
import com.fit.WatchShop.interfaces.WatchWorker;
import com.fit.WatchShop.service.Buyers;
import com.fit.WatchShop.service.Buys;
import com.fit.WatchShop.service.WatchWorkers;
import com.fit.WatchShop.service.Watches;

public class EntryBuy {

	@SessionState
	private User user;

	private boolean userExists;
	
	public User getUser() {
		return user;
	}
	
	Object onActivate() {
		if (!userExists) return Login.class;
		return null;
	}
	@Persist
	private String buyBuyer;
	@Persist
	private String buyWatchWorker;
	@Persist
	@Validate("required, minlength=3")
	private String buyWatch;
	
	@InjectPage
	private ViewBuy viewBuyPage;
	@InjectPage
	private ViewError viewErrorPage;

	@Inject
	private Watches watches;
	@Inject
	private Buyers buyers;
	@Inject
	private WatchWorkers watchWorkers;
	@Inject
	private Buys buys;
	
	public Collection<Watch> getWatches() {
		return watches.retrieve();
	}

	public Collection<Buyer> getBuyers() {
		return buyers.retrieve();
	}

	public Collection<WatchWorker> getWatchWorkers() {
		return watchWorkers.retrieve();
	}

	@OnEvent(value = "submit", component = "buyInputForm")
	Object onFromSubmit() {
		
		this.buyWatchWorker = "WebApp";
		this.buyBuyer = getUser().getUserName();

		Watch watch = null;
		Buyer buyer = null;
		WatchWorker watchWorker= null;

		try {
			buyer = buyers.retrieveSingleBuyer(getBuyBuyer());
			watchWorker = watchWorkers
					.retrieveSingleWatchWorker(getBuyWatchWorker());
			watch= watches.retrieveSingleWatch(getBuyWatch());
		} catch (Exception ex) {
			viewErrorPage.setPassedMessage(ex.toString());
			return viewErrorPage;
		}
		
		if (buyer == null) {
			viewErrorPage
					.setPassedMessage("Greska u prepoznavanju objekta Buyer u Map-i");
			return viewErrorPage;
		}
		if (watchWorker == null) {
			viewErrorPage
					.setPassedMessage("Greska u prepoznavanju objekta Watch Worker u Map-i");
			return viewErrorPage;
		}
		if (watch == null) {
			viewErrorPage
					.setPassedMessage("Greska u prepoznavanju objekta Watch u Map-i");
			return viewErrorPage;
		}

		Buy buyBean = new BuyBean();
		buyBean.executeBuyCreation(buyer, watchWorker, watch, buys);

		this.buyWatchWorker= "";
		this.buyBuyer = "";
		this.buyWatch = "";
		
		return viewBuyPage;
	}

	public String getBuyBuyer() {
		return buyBuyer;
	}

	public void setBuyBuyer(String buyBuyer) {
		this.buyBuyer = buyBuyer;
	}

	public String getBuyWatchWorker() {
		return buyWatchWorker;
	}

	public void setBuyWatchWorker(String buyWatchWorker) {
		this.buyWatchWorker = buyWatchWorker;
	}

	public String getBuyWatch() {
		return buyWatch;
	}

	public void setBuyWatch(String buyWatch) {
		this.buyWatch = buyWatch;
	}
	

}
